package sm.routine;

import java.util.TimerTask;

import sm.namingservice.ServerState;
import sm.namingservice.ServerTable;
import sm.namingservice.ServerTuple;
import sm.routine.workload.WorkLoad;
import sm.server.serverlist.ServerList;
import sm.server.vmOperations.DwsDataRetriever;
import sm.utils.PropertySet;

import com.vmware.vim25.DynamicProperty;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ObjectContent;
import com.vmware.vim25.RetrieveResult;
import com.vmware.vim25.ServiceContent;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.VimServiceLocator;

public class CheckGwsPowerState extends TimerTask{
	private ManagedObjectReference _svcRef;
	private VimServiceLocator _locator;
	private VimPortType _service;
	private ServiceContent _sic;
	
	private ServerTable _st;
	private ServerState _ss;
	private WorkLoad _wl;
	
	public CheckGwsPowerState(ServerTable _st, ServerState ss,WorkLoad wl,ManagedObjectReference _svcRef,VimServiceLocator _locator,
			VimPortType _service,ServiceContent _sic){
		this._st = _st;
		this._ss = ss;
		this._wl = wl;
		this._svcRef = _svcRef;
		this._locator = _locator;
		this._service = _service;
		this._sic = _sic;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Thread:"+Thread.currentThread().getId()+"CheckGwsPowerState.");
		String[] dwsList = ServerList.getASList();
		RetrieveResult vmprops;
		try {
			vmprops = DwsDataRetriever.retrieveData(
					this._sic, this._service,
					new String[]{"VirtualMachine"},"Virtual Lab/vm/cs6282/studentVM",
					new String[]{"name"});

			for(ObjectContent oc:vmprops.getObjects()){
				for(DynamicProperty dp:oc.getPropSet()){
					if(dp.getName().equals("name")){
						String name = (String)dp.getVal();
						for(String s:dwsList){
							if(s.equals(name)){
								String curPs = getPs(name);
								if(!curPs.equals(_ss.getDwsState(name))){
									_ss.setDwsState(name, curPs);
									_wl.reset(name);
								}
							}
						}

					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String getPs(String name){
		String result = null;
		try {
			RetrieveResult psprops= DwsDataRetriever.retrieveData(
					this._sic, this._service,
					new String[]{"VirtualMachine"},"Virtual Lab/vm/cs6282/studentVM",
					new String[]{"name","runtime.powerState"});
			for(ObjectContent oc:psprops.getObjects()){
				boolean next = false;
				for(DynamicProperty dp:oc.getPropSet()){
					if(dp.getName().equals("name")){
						if(dp.getVal().equals(name)){
							next = true;
						}
					}
					if(dp.getName().equals("runtime.powerState")){
						if(next){
							result = dp.getVal().toString();
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	

		return result;
	}
}
